#include "mgenre.h"

MGenre::MGenre(QObject *parent) :
    QAbstractTableModel(parent)
{
}

MGenre::~MGenre()
{
}

Qt::ItemFlags MGenre::flags(const QModelIndex& index) const
{
    Qt::ItemFlags theFlags = QAbstractTableModel::flags(index);
    if (index.isValid())
    {
        if(index.column() == DtGenre::Name)
            theFlags |= Qt::ItemIsSelectable;
    }
    return theFlags;
}

QVariant MGenre::data(const QModelIndex& index, int role) const
{
  if (!index.isValid() ||
      index.row() < 0 || index.row() >= mGenre.size() ||
      index.column() < 0 || index.column() >= DtGenre::NbAttr)
    return QVariant();
  const DtGenre *genre = mGenre.at(index.row());
  if(role == Qt::DisplayRole || role == Qt::EditRole)
  {
      switch(index.column())
      {
      case DtGenre::Play:
          return genre->getPlay();
      case DtGenre::Add:
          return genre->getAdd();
      case DtGenre::Name:
          return genre->getName();
      case DtGenre::Heart:
          return genre->getHeartDelegate();
      default: ;
      }
  }
  if(role == Qt::ToolTipRole)
      return genre->getName();

  return QVariant();
}

QVariant MGenre::headerData(int section, Qt::Orientation orientation,
                             int role) const
{
    if (role != Qt::DisplayRole)
        return QVariant();
    if (orientation == Qt::Horizontal && section < DtGenre::NbAttr)
    {
        if(section == DtGenre::Name)
            return QObject::tr("Name");
    }
    return "";
}

int MGenre::rowCount(const QModelIndex & parent) const
{
    return parent.isValid() ? 0 : mGenre.size();
}

int MGenre::columnCount(const QModelIndex & parent) const
{
    return parent.isValid() ? 0 : DtGenre::NbAttr;
}

bool MGenre::insertRows(int , int , const QModelIndex&)
{
    return false;
}

bool MGenre::removeRows(int row, int count, const QModelIndex&)
{
  beginRemoveRows(QModelIndex(), row, row + count - 1);
  for (int i = 0; i < count; ++i)
    mGenre.removeAt(row);
  endRemoveRows();
  return true;
}

QList<QHeaderView::ResizeMode> MGenre::getHorizontalResizeModes() const
{
    QList<QHeaderView::ResizeMode> t;
    t.append(QHeaderView::Fixed);
    t.append(QHeaderView::Fixed);
    t.append(QHeaderView::Stretch);
    t.append(QHeaderView::Fixed);
    return t;
}

void MGenre::setList(QList<DtGenre *> &lGenre)
{
    mGenre = lGenre;
    reset();
}

QList<DtGenre *> MGenre::getList() const
{
    return mGenre;
}
